<?php
class editor extends panel {
	var $name = 'File Editor';
	
	public static function panel_header() {
		?>
		<script type="text/javascript" src="<?=CN_URL?>includes/third-party/jquery-linedtextarea/jquery-linedtextarea.js"></script>
		<link href="<?=CN_URL?>includes/third-party/jquery-linedtextarea/jquery-linedtextarea.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript">
			$(document).ready(function() {
				$("#code").linedtextarea();
				$('#code').keydown(function(event) {
					if(event.keyCode == '9') {
						event.preventDefault();
						alert('tab');
					}
					
				});
			});
		</script>
		<?php
	}
	
	public static function display() {
		global $cn_current_user; 
		if($_REQUEST['save_file']) {
			/** Make sure the user is an administrator */
			if(cn_user_permissions_check('global_admin', $cn_current_user)) {
				$fh = fopen($_REQUEST['file_path'], 'w') or die("can't open file");
				$write = str_replace('    ', "\t", stripslashes($_REQUEST['file_contents']));
				fwrite($fh, $write);
				fclose($fh);
				?><p class="alert-success">File saved.</p><?php
			}
		}
		?>
		<div class="two-column">
			<div class="box">
				<h3>Templates</h3>
				<div class="box-content file-list">
					
					<ul>
						<?php self::display_folder_array(get_files(CN_DOCUMENT_ROOT . 'templates', array('php', 'js', 'css', 'html', 'htm')), 'templates'); ?>
					</ul>
					
				</div>
			</div>
		</div>
		<div class="two-column">
			<div class="box">
				<h3>Modules</h3>
				<div class="box-content file-list">
					<ul>
						<?php self::display_folder_array(get_files(CN_DOCUMENT_ROOT . 'modules', array('php', 'js', 'css', 'html', 'htm')), 'modules'); ?>
					</ul>
				</div>
			</div>
		</div>
		<div class="clearboth"></div>
		<div class="one-column">
			<div class="box">
				<?php if($file = $_REQUEST['file']) : ?>
				<h3><a name="edit_form"></a>Edit File: <b>/<?=$file?></b></h3>
				<div class="box-content">
				<small><i>Note: 4 spaces in a row will be converted to a tab character</i></small>
				<form action="<?=$_SERVER['PHP_SELF']?><?= query_string();?>" method="POST">
				<input type="hidden" value="<?=CN_DOCUMENT_ROOT . $file?>" name="file_path"/>
				<div class="box-content">
					<textarea wrap="off" name="file_contents" id="code" class="file-editor"><?= str_replace("\t", "    ", htmlentities(file_get_contents(CN_DOCUMENT_ROOT . $file))); ?>
					</textarea>
				</div>
				<input type="submit" name="save_file" class="big-button wide" value="Save"/>
				</form>
				</div>
				<?php else : ?>
				<h3>Select a file to edit.</h3>
				<?php endif;?>
			</div>
		</div>
		
		<?php
	}
	
	private static function display_folder_array($array, $parent = null) {
		foreach($array as $src=>$file) {
			if(is_array($file)) {
				if(count($file)) {
					echo '<li class="folder">'. $src;
					echo '<ul>';
					self::display_folder_array($file, $parent.'/'.$src);
					echo '</ul>';
					echo '</li>';
				}
			}
			else {
				echo '<li class="file">';
				$file = str_replace(CN_DOCUMENT_ROOT, '', $parent.'/'.$file);
				$pos = strposall($file, '/');
				$pos = $pos[count($pos) - 1];
				$name = substr($file, $pos + 1);
				set_temp_query_var('file', $file);
				echo '<a href="'.$_SERVER['PHP_SELF'].query_string().'#edit_form">'.$name.'</a>';
				echo '</li>';
			}
		}
	}
}
?>